import { Pipe, PipeTransform } from '@angular/core';

/**
 * Generated class for the HtmlEncodePipe pipe.
 *
 * See https://angular.io/api/core/Pipe for more info on Angular Pipes.
 */
@Pipe({
  name: 'utils',
})
export class UtilsPipe implements PipeTransform {
  /**
   * Takes a value and makes it lowercase.
   */
  transform(value: string, pipeName: string, ...args) {
      switch(pipeName) {
        case 'htmlEncode':
        return this.htmlEncode(value);
        case 'getTime':
        return this.getTime(value);
        default:
        return "";
      }
  }

  htmlEncode(value) {
    var text = value.replace(/<\/?.+?>/g,"").replace(/ /g,"").replace(/&nbsp;/ig, "");
    return text;
  }

  getTime(value) {
    var now = Date.parse(Date()) / 1000;
    var limit = now - value;
    var content;
    if (limit < 60) {
        content = "刚刚";
    } else if (limit >= 60 && limit < 3600) {
        content = Math.floor(limit / 60) + "分钟前";
    } else if (limit >= 3600 && limit < 86400) {
        content = Math.floor(limit / 3600) + "小时前";
    } else {
        content = this.timeStamp2String(value, "md2");
    }
    return content;
  }

  //时间处理-时间轴=>年月日格式
  timeStamp2String(time, id) {
    //y=年;ymd=年月日;ymdhm年月日时分;ymdhms年月日时分秒
    var datetime = new Date(parseInt(time) * 1000);
    //datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var month2 = datetime.getMonth() + 1 < 10 ? datetime.getMonth() + 1 : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var date2 = datetime.getDate() < 10 ? datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    if (id == "y") {
        return year;
    } else if (id == "ym") {
        return year + "-" + month;
    } else if (id == "ymd") {
        return year + "-" + month + "-" + date;
    } else if (id == "ymd2") {
        return year + "年" + month + "月" + date + "日";
    } else if (id == "ymdhm") {
        return year + "-" + month + "-" + date + " " + hour + ":" + minute;
    } else if (id == "ymdhms") {
        return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    } else if (id == "mdhms") {
        return month + "-" + date + " " + hour + ":" + minute + ":" + second;
    } else if (id == "md") {
        return month + "-" + date;
    } else if (id == "hm") {
        return hour + ":" + minute;
    } else if (id == "xs") {
        return hour + ":" + hour;
    } else if (id == "md2") {
        return month2 + "月" + date2 + "日";
    } else if (!id) {
        return year + "-" + month + "-" + date;
    } else if (id == "mdhm") {
        return month + "-" + date + " " + hour + ":" + minute;
    } else if (id == "md3") {
        //仿微信
        return "<span>" + month2 + "  </span><span style='font-size:.7em;'>" + date2 + "日</span>";
    }
}

}